<template>
  <view class="body">
    <view>
      <view class="name titleBold">{{ info.name }}</view>
      <view v-if="info.tips" class="tips"> {{ info.tips }} </view>
      <view class="sickList">
        <view
          v-for="item in itemData"
          :key="item.id"
          :class="[info.rowBlock === true ? 'sickBox blocksInl' : 'sickBox']"
        >
          <view :class="[item.active ? 'sickSpan cur' : 'sickSpan']" @click="choseList(item)">
            {{ item.name }}
            <span v-if="item.active" class="curImg"><img :src="curIcon" /></span>
          </view>
          <view v-if="item.tips" class="kidTips">{{ item.tips }}</view>
        </view>
      </view>
      <view v-if="checkValue === otherNum" class="inputdisplay">
        <!-- <view><span class="name">其他</span></view> -->
        <view class="displayInput">
          <textarea
            v-model="mainData.exerciseModeOther"
            class="uni-textarea"
            maxlength="20"
            placeholder-class="phcolor"
          />
        </view>
        <view class="clear"></view>
      </view>
    </view>
  </view>
</template>

<script>
import curIcon from '@/static/icon/cur.png';
export default {
  props: {
    // 是否展示搜索按钮
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      checkValue: undefined,
      curIcon: curIcon,
      noChose: false,
      mainData: {
        exerciseModeOther: ''
      },
      itemData: [],
      otherNum: ''
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    }
  },
  mounted() {
    let data = JSON.parse(JSON.stringify(this.info.options));
    data.map((item) => {
      item.active = false;
      return item;
    });
    this.itemData = data;
    this.otherNum = data[data.length - 1].id;
  },
  methods: {
    validate() {
      // 疾病直接提交接口
      // 必填校验
      let check = this.itemData.some((item) => item.active);
      console.log('运动方式:', JSON.stringify(this.itemData));
      if (this.info.require && !check) {
        uni.showToast({ title: `请选择${this.info.name}`, icon: 'none' });
        return false;
      }
      let subData = [];
      this.itemData.forEach((item) => {
        if (item.active) {
          subData.push(item.id);
        }
      });
      if (this.checkValue !== this.otherNum) {
        this.mainData.exerciseModeOther = '';
      }
      if (this.checkValue === this.otherNum && !this.mainData.exerciseModeOther) {
        uni.showToast({ title: `请输入其他运动方式`, icon: 'none' });
        return false;
      }
      let submitData = this.assessForm;
      // let curActive = this.active + 1;
      submitData[this.info.key] = subData;
      submitData.exerciseModeOther = this.mainData.exerciseModeOther || '';
      // 数组求和，因为后端不支持数组
      let numbers = submitData[this.info.key];
      submitData[this.info.key] = numbers.reduce(function (sum, number) {
        // sum2 前两个数的和
        return sum + number;
      }, 0);
      // this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      // this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
      return {
        [this.info.key]: submitData[this.info.key],
        exerciseModeOther: submitData.exerciseModeOther
      };
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    choseList(val) {
      this.checkValue = '';
      console.log('运动方式 val:', val);
      const indexs = this.itemData.findIndex((item) => item.id === val.id);
      this.itemData[indexs].active = !this.itemData[indexs].active;
      let otherCheck = this.itemData.some((item) => item.active && item.id === this.otherNum);
      console.log('this.itemData:', this.itemData);
      if (otherCheck) {
        this.checkValue = this.otherNum;
      }
      this.$emit('choseList', this.info.key, this.validate);
    },
    inputChange(e) {}
  }
};
</script>
<style lang="scss">
$black: rgab(0, 0, 0, 0.55);
.body {
  padding: 24rpx;
  .tips {
    color: rgab(0, 0, 0, 0.55);
    font-size: 28rpx;
    margin-bottom: 20rpx;
  }
  .sickList {
    .sickBox {
      display: inline-block;
      width: 50%;
      .sickSpan {
        display: inline-block;
        text-align: center;
        width: 80%;
        padding: 10rpx 18rpx;
        margin: 12rpx 16rpx;
        border: 2rpx solid rgba(178, 178, 178, 1);
        border-radius: 16rpx;
        font-size: 28rpx;
        position: relative;
        .curImg {
          position: absolute;
          width: 40rpx;
          height: 40rpx;
          right: 0;
          bottom: 0;
          border: none;
          img {
            width: 40rpx;
            height: 40rpx;
          }
        }
      }
      .cur {
        border: 2rpx solid rgba(242, 85, 89, 1);
        color: rgba(242, 85, 89, 1);
      }
      .kidTips {
        color: $black;
        font-size: 28rpx;
        margin-bottom: 30rpx;
      }
    }
    .blocks {
      display: block;
      width: 100%;
      .sickSpan {
        width: 590rpx;
      }
    }
    .blocksInl {
      display: inline-block;
      width: auto;
      .sickSpan {
        width: auto;
      }
    }
  }
}
.inputBox {
  position: relative;
  padding: 20rpx 0;
  .title {
    float: left;
  }
  .name {
    float: left;
    width: 140rpx;
    color: $black;
  }
  .uni-input {
    float: right;
    text-align: right;
    margin-right: 40rpx;
    width: 186rpx;
    padding: 0 12rpx;
    background: rgba(244, 244, 244, 1);
  }
  .unit {
    position: absolute;
    right: 0;
    top: 30rpx;
    color: $black;
    font-size: 28rpx;
  }
}
.inputdisplay {
  margin: 6rpx;
  .displayInput {
    .uni-textarea {
      border: 1px solid #eee;
      border-radius: 16rpx;
      width: 100%;
      padding: 10rpx 20rpx;
      height: 84rpx;
      width: 590rpx;
      font-size: 28rpx;
    }
  }
}
</style>
